package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.sql.SQLOutput;

@Aspect //切面类
@Component
public class CalaTimeAspect {
    @Pointcut("@annotation(com.itheima.flag.Flag)")
    public void pt(){}

//    @Around("execution(* com.itheima.controller.DeptController.*(..))") //环绕通知
    public Object calaTime(ProceedingJoinPoint joinPoint) throws Throwable {

        //前增强
        long start = System.currentTimeMillis(); //获取当前时间
        Object result = joinPoint.proceed();    //调用目标方法
        //后增强
        long end = System.currentTimeMillis(); //这是调用方法后的时间
        //获取方法名
        String method = joinPoint.getSignature().getName();
        System.out.println(method + "耗时" + (end - start) + "毫秒");
        return result;
    }
    /*过程:
    before  在方法执行前先执行
    After   在方法执行后执行
    AfterReturning  方法执行后通知, 有异常不执行
    AfterThrowing   有异常才执行
    Around  环绕通知, 可以执行上述所有
    * */
    @Around("pt()")
    public Object method(ProceedingJoinPoint joinPoint) throws Throwable {
        //提高作用域
        Object result = null;
        try {
            System.out.println("before...");
            //手动调用目标资源执行
            result = joinPoint.proceed();
            System.out.println("AfterReturing...");
        } catch (Throwable e) {
            System.out.println("AfterThrowing...");
        }finally{
            System.out.println("After...");
        }//最后返回目标方法执行完毕结果

        return result;
    }


}
